﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Deeper.Common;
using Deeper.Common.Web;
using Deeper.DataAccess;
using ZhongTian.Model;
using ZhongTian.Services;


namespace ZhongTian.Web
{
	public partial class ItemsInfo : Page
	{
		protected void btnSearch_Click(object sender, EventArgs e)
		{
			rptList.ReBind();
		}
		private string OrderBy { get { return ZhongTian.Model.Items.F.ItemId.Desc.ToString(); } }

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				rptList.ReBind();
				IList<ItemClass> list = new DataContext().GetList<ItemClass>();
				ddlClassId.BindList(list, "ItemClassName", "ItemClassId", "无类别", "0");
				ddlKeytype.BindList(list, "ItemClassName", "ItemClassId", "无类别", "0");
			}
		}

		protected PageResult MyDataBind(int pageIndex, int pageSize)
		{
			string key = txtKeyWord.Text;
			WhereClip where = (ZhongTian.Model.Items.F.itemName.Contains(key) | ZhongTian.Model.Items.F.Py.Contains(key));
			if (ddlKeytype.SelectedIndex > 0)
				where = where.Bracket() & ZhongTian.Model.Items.F.ClassId == ddlKeytype.SelectedValue;
			return new DataContext().GetPageList<ZhongTian.Model.Items>(pageIndex, pageSize, where, orderby: OrderBy);
		}


		protected void ModelAdd()
		{
			pnlDetail.GetEdit(new ZhongTian.Model.Items());
		}

		protected void ModelSave()
		{
			"保存".InfoAction(() =>
			{
				int id = hfItemId.Value.ToInt();
				ZhongTian.Model.Items model = id == 0 ? new ZhongTian.Model.Items() : new DataContext().GetEntityById<ZhongTian.Model.Items>(id);
				model.GetData(pnlDetail);
				model.Py = model.itemName.GetPyCode();
				model.Save();
				pnlDetail.Hide();
				rptList.ReBind();
			});
		}


		protected void rptList_Command(object sender, RepeaterCommandEventArgs e)
		{
			int id = e.CommandArgument.ToString().ToInt();
			string cmd = e.CommandName.ToLower();
			switch (cmd)
			{
				case "delete":
					"删除".InfoAction(() =>
					{
						new DataContext().Delete<ZhongTian.Model.Items>(id);
						rptList.ReBind();
					});
					break;
				case "edit":
					"获取编辑信息".InfoAction(() =>
					  {
						  pnlDetail.GetEdit(new DataContext().GetEntityById<ZhongTian.Model.Items>(id));
					  }, false);
					break;
			}

		}
	}


}